<!-- 采购订单 -->
<template>
  <div
    class="purchase_batch"
    v-if="!appStore.isPhone">
    <el-card
      class="search_card"
      ref="searchCardRef">
      <el-form
        :model="formData"
        class="search_form">
        <el-row>
          <el-form-item label="资金仓库">
            <el-select
              v-model="formData.baseCapitalWarehouseId"
              placeholder="资金仓库"
              style="width: 150px"
              filterable
              clearable>
              <el-option
                v-for="item in capitalPartyList"
                :key="item.id"
                :label="item.name"
                :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="米厂信息">
            <el-select
              v-model="formData.baseRiceBarnId"
              placeholder="米厂信息"
              style="width: 150px"
              filterable
              clearable>
              <el-option
                v-for="item in baseRiceBarnList"
                :key="item.id"
                :label="item.simple_name"
                :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="采购订单名称">
            <el-input
              v-model="formData.purchaseOrderName"
              style="width: 150px"
              placeholder="采购订单名称"
              clearable>
              <template #prefix>
                <el-icon>
                  <search />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-button
            type="primary"
            @click="getList(formData)"
            class="query_button">
            <template #icon>
              <el-icon>
                <search />
              </el-icon>
            </template>
            {{ config.button.text.query }}
          </el-button>
          <el-button @click="reset">
            <template #icon>
              <el-icon>
                <Refresh />
              </el-icon>
            </template>
            {{ config.button.text.reset }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="table_card">
      <Table
        :data="tableData"
        v-loading="tableLoading"
        :height="height">
        <el-table-column
          prop="purchaseOrderNumber"
          label="采购订单编号"
          min-width="120"
          :resizable="false" />
        <el-table-column
          prop="purchaseOrderName"
          label="采购订单名称"
          min-width="120"
          :resizable="false" />
        <el-table-column
          prop="serialNumber"
          label="编号"
          min-width="100"
          :resizable="false" />
        <el-table-column
          prop="baseCapitalWarehouseName"
          label="资金仓库简称"
          min-width="120"
          :resizable="false" />
        <el-table-column
          prop="baseRiceBarnSimpleName"
          label="米厂名称"
          min-width="120"
          :resizable="false" />
        <el-table-column
          prop="basePickUnitPrice"
          label="基础提货单价"
          min-width="120"
          :resizable="false" />
        <el-table-column
          prop="purchaseSumTon"
          label="采购总吨数"
          min-width="100"
          :resizable="false" />
        <el-table-column
          prop="sendSumTon"
          label="发货总吨数"
          min-width="100"
          :resizable="false" />
        <el-table-column
          prop="arrivalSumTon"
          label="到货总吨数"
          min-width="100"
          :resizable="false" />
        <el-table-column
          prop="purchaseDate"
          label="采购日期"
          min-width="120"
          :resizable="false">
          <template #default="scope">
            {{ formatDate(scope.row.purchaseDate) }}
          </template>
        </el-table-column>
        <el-table-column
          label="启用状态"
          min-width="100"
          :resizable="false">
          <template #default="scope">
            <el-tag :type="scope.row.isEnable ? 'primary' : 'danger'">
              {{ scope.row.isEnable ? config.button.text.enable
                : config.button.text.stop }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="250"
          fixed="right"
          :resizable="false">
          <template #default="scope">
            <el-button
              type="warning"
              v-permission="validateButtonPermission(route, 1)"
              @click="editEvent(scope.row)">
              {{ config.button.text.edit }}
            </el-button>
            <el-button
              type="success"
              @click="viewEvent(scope.row)">
              {{ config.button.text.view }}
            </el-button>
            <el-dropdown v-permission="validateButtonPermission(route, 2)">
              <span class="el-dropdown-link">
                {{ config.button.text.more }}<el-icon class="el-icon--right"><arrow-down /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="addTrain(scope.row)">添加车次</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
        </el-table-column>
      </Table>
      <Pagination
        :pageNum="pageNum"
        :pageSize="pageSize"
        :total="total"
        @handle-size-change="(val) => pageSize = val"
        @handle-current-change="(val) => pageNum = val" />
    </el-card>
    <!-- 查看 -->
    <View
      ref="viewRef"
      :rowData="rowData" />
  </div>
  <!-- 手机端页面 -->
  <H5Index v-else></H5Index>
</template>

<script setup>
import { ref, onMounted } from "vue";
import Pagination from "@/components/Pagination.vue"
import Table from "@/components/Table.vue"
import ApiPurchaseOrder from "@/api/modules/purchase-order"
import { useTable } from "@/hooks/useTable"
import config from "@/common/element-plus/config";
import app from "@/common/config/app";
import { validateButtonPermission } from "@/common/utils/validate";
import { useRoute, useRouter } from "vue-router"
import { useAppStore } from "@/stores/app"
import { storeToRefs } from "pinia";
import { formatDate } from "@/common/utils/date";
import ApiBaseCapitalWarehouse from "@/api/modules/capital-ware-house"
import ApiBaseRiceBarn from "@/api/modules/base-rice-barn"
import View from "./component/View.vue"
import H5Index from "./H5Index.vue";

// 路由
const route = useRoute()
const router = useRouter()
// 查询表单
const formData = ref({})
// 查看组件实例
const viewRef = ref()
// 表格每行数据
const rowData = ref({})
// 表格高度(固定表头)
const height = ref(0)
// 查询卡片实例
const searchCardRef = ref()
// 资金仓库简称列表
const capitalPartyList = ref([])
// 米厂简称列表
const baseRiceBarnList = ref([])

const { tableMethods, tableState, pagination } = useTable({
  fetchDataApi: async (data) => {
    const { pageSize, pageNum } = pagination
    const res = await ApiPurchaseOrder.selectPage(data, {
      pageNum: pageNum.value,
      pageSize: pageSize.value,
    })
    return {
      list: res.data.records,
      total: res.data.total
    }
  }
})
const { getList } = tableMethods
const { pageSize, pageNum } = pagination
const { total, tableLoading, tableData } = tableState

// 重置
const reset = () => {
  formData.value = {}
  getList()
}

// 打开查看对话框
const viewEvent = (row) => {
  viewRef.value.visibility = true
  rowData.value = row
}

// 打开修改对话框
const editEvent = (row) => {
  router.push({
    path: "/purchase-order-insert/index",
    query: { purchaseOrderNumber: row.purchaseOrderNumber }
  })
}

// 添加车次
const addTrain = (row) => {
  router.push({
    path: "/train-inventory-insert/index",
    query: { purchaseOrderNumber: row.purchaseOrderNumber } 
  })
}

// 监听查询卡片大小变化
const resizeObserver = new ResizeObserver(entries => {
  for (const entry of entries) {
    if (contentFullScreen.value) {
      height.value = (window.innerHeight - app.otherHeight - app.tagHeight - entry.contentRect.height) + "px"
    } else {
      height.value = (window.innerHeight - app.otherHeight - app.tagHeight - app.headerHeight - entry.contentRect.height) + "px"
    }
  }
})

const appStore = useAppStore()

// 保持响应式
const { contentFullScreen } = storeToRefs(appStore)

onMounted(async () => {
  if (appStore.isPhone) return
  resizeObserver.observe(searchCardRef.value.$el)
  let data = await ApiBaseCapitalWarehouse.getSimpleNameList("/baseCapitalWarehouse/")
  capitalPartyList.value = data.data
  data = await ApiBaseRiceBarn.getSimpleNameList()
  baseRiceBarnList.value = data.data
})
</script>

<style lang="scss" scoped>
.purchase_batch {
  .search_card {
    margin-bottom: 4px;

    :deep(.el-card__body) {
      padding: 10px;
      display: flex;
      justify-content: space-between;
    }

    .el-button {
      margin: 5px 10px 5px 0;
    }

    .query_button {
      margin-left: 100px;
    }

    .el-form-item {
      margin-right: 10px;
      padding: 5px 0;
    }
  }

  .table_card {
    :deep(.el-card__body) {
      padding: 0;
    }

    .el-dropdown-link {
  cursor: pointer;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
  outline: none;
  margin: 5px 10px 5px 10px;
}
  }
}
</style>